package com.sddnytsh.body_buildingsystem.screens

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.material.icons.filled.KeyboardArrowUp
import androidx.compose.material.icons.filled.Warning
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.Divider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController

@Composable
fun HelpAndFeedbackScreen(navController: NavController) {
    var expandedSections by remember { mutableStateOf(mutableSetOf<Int>()) }
    var feedbackText by remember { mutableStateOf("") }
    var contactEmail by remember { mutableStateOf("") }
    var showSuccessDialog by remember { mutableStateOf(false) }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.White)
    ) {
        // 顶部栏
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .height(56.dp)
        ) {
            // 返回按钮
            IconButton(
                onClick = { navController.popBackStack() },
                modifier = Modifier
                    .align(Alignment.CenterStart)
                    .padding(start = 8.dp)
            ) {
                Icon(
                    imageVector = Icons.Default.ArrowBack,
                    contentDescription = "返回",
                    tint = Color.Black
                )
            }

            // 标题
            Text(
                text = "帮助与反馈",
                color = Color.White,
                modifier = Modifier.align(Alignment.Center)
            )
        }

        // 内容区域
        LazyColumn(
            modifier = Modifier
                .weight(1f)
                .padding(16.dp),
            verticalArrangement = Arrangement.spacedBy(24.dp)
        ) {
            item {
                // 欢迎提示
                Card(
                    modifier = Modifier.fillMaxWidth(),
                ) {
                    Column(modifier = Modifier.padding(16.dp)) {
                        Text(
                            text = "需要帮助？",
                        )
                        Spacer(modifier = Modifier.height(8.dp))
                        Text(
                            text = "请查看常见问题或提交反馈，我们会尽快为您解决",
                            style = MaterialTheme.typography.bodyMedium,
                            color = Color.Gray
                        )
                    }
                }
            }

            item {
                // 常见问题
                Text(
                    text = "常见问题",
                )
            }

            // 常见问题列表
            faqItems.forEachIndexed { index, faq ->
                item {
                    FAQItem(
                        question = faq.question,
                        answer = faq.answer,
                        isExpanded = expandedSections.contains(index),
                        onExpandToggle = {
                            // 创建新的Set来触发重组
                            val newSet = expandedSections.toMutableSet()
                            if (newSet.contains(index)) {
                                newSet.remove(index)
                            } else {
                                newSet.add(index)
                            }
                            expandedSections = newSet
                        }
                    )
                }
            }

            item {
                // 反馈表单
                Text(
                    text = "意见反馈",
                    modifier = Modifier.padding(top = 16.dp)
                )

                Card(
                    modifier = Modifier.fillMaxWidth(),
                ) {
                    Column(modifier = Modifier.padding(16.dp)) {
                        Text(
                            text = "告诉我们您的想法",
                            style = MaterialTheme.typography.bodyLarge.copy(fontWeight = FontWeight.Medium)
                        )
                        Spacer(modifier = Modifier.height(16.dp))

                        // 反馈内容
                        OutlinedTextField(
                            value = feedbackText,
                            onValueChange = { feedbackText = it },
                            label = { Text("请输入您的反馈意见") },
                            modifier = Modifier
                                .fillMaxWidth()
                                .height(120.dp),
                            singleLine = false,
                            maxLines = 5
                        )

                        Spacer(modifier = Modifier.height(16.dp))

                        // 联系方式
                        OutlinedTextField(
                            value = contactEmail,
                            onValueChange = { contactEmail = it },
                            label = { Text("联系方式（邮箱/电话）") },
                            modifier = Modifier.fillMaxWidth(),
                            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Email)
                        )

                        Spacer(modifier = Modifier.height(16.dp))

                        // 提交按钮
                        Button(
                            onClick = {
                                if (feedbackText.isNotBlank()) {
                                    // 这里可以添加提交逻辑
                                    showSuccessDialog = true
                                    feedbackText = ""
                                    contactEmail = ""
                                }
                            },
                            modifier = Modifier
                                .fillMaxWidth()
                                .height(48.dp),
                            enabled = feedbackText.isNotBlank()
                        ) {
                            Text("提交反馈")
                        }
                    }
                }
            }

            item {
                // 紧急联系
                Card(
                    modifier = Modifier
                        .fillMaxWidth()
                        .background(
                            Color(0xFFFFF8E1)
                        ),
                ) {
                    Column(modifier = Modifier.padding(16.dp)) {
                        Row(verticalAlignment = Alignment.CenterVertically) {
                            Icon(
                                imageVector = Icons.Filled.Warning,
                                contentDescription = "紧急联系",
                                tint = Color(0xFFFFA000),
                                modifier = Modifier.size(20.dp)
                            )
                            Spacer(modifier = Modifier.width(8.dp))
                            Text(
                                text = "紧急联系",
                                style = MaterialTheme.typography.bodyLarge.copy(fontWeight = FontWeight.Bold),
                                color = Color(0xFFFFA000)
                            )
                        }
                        Spacer(modifier = Modifier.height(8.dp))
                        Text(
                            text = "客服电话：400-123-4567",
                            style = MaterialTheme.typography.bodyMedium
                        )
                        Text(
                            text = "服务时间：9:00-18:00",
                            style = MaterialTheme.typography.bodySmall,
                            color = Color.Gray
                        )
                    }
                }
            }
        }
    }

    // 提交成功对话框
    if (showSuccessDialog) {
        AlertDialog(
            onDismissRequest = { showSuccessDialog = false },
            title = {
                Text("提交成功")
            },
            text = {
                Text("感谢您的反馈，我们会尽快处理并回复您！")
            },
            confirmButton = {
                Button(
                    onClick = { showSuccessDialog = false }
                ) {
                    Text("确定")
                }
            }
        )
    }
}

// 常见问题数据类
data class FAQItemData(
    val question: String,
    val answer: String
)

// 常见问题列表
val faqItems = listOf(
    FAQItemData(
        "如何查看历史数据？",
        "进入\"统计\"页面，您可以查看每天的运动数据图表和历史记录。"
    ),
    FAQItemData(
        "运动数据同步失败怎么办？",
        "请检查网络连接，如果问题持续存在，可以尝试退出应用重新登录，或联系客服人员。"
    ),
    FAQItemData(
        "如何修改个人资料？",
        "点击右下角\"我的\"进入个人中心，点击头像或个人信息即可进行修改。"
    )
)

// 常见问题项组件
@Composable
fun FAQItem(question: String, answer: String, isExpanded: Boolean, onExpandToggle: () -> Unit) {
    Card(
        modifier = Modifier.fillMaxWidth(),
    ) {
        Column {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .clickable { onExpandToggle() }
                    .padding(16.dp),
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Text(
                    text = question,
                    style = MaterialTheme.typography.bodyLarge.copy(fontWeight = FontWeight.Medium),
                    modifier = Modifier.weight(1f)
                )
                Icon(
                    imageVector = if (isExpanded) Icons.Filled.KeyboardArrowUp else Icons.Filled.KeyboardArrowDown,
                    contentDescription = if (isExpanded) "收起" else "展开",
                    tint = Color.Gray
                )
            }

            if (isExpanded) {
                Divider(modifier = Modifier.padding(horizontal = 16.dp))
                Text(
                    text = answer,
                    style = MaterialTheme.typography.bodyMedium,
                    color = Color.Gray,
                    modifier = Modifier.padding(16.dp)
                )
            }
        }
    }
}